package com.mobile.liuyi_s.orderlunch.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.mobile.liuyi_s.orderlunch.MyApplication;
import com.mobile.liuyi_s.orderlunch.R;
import com.mobile.liuyi_s.orderlunch.Utils.Utils;
import com.mobile.liuyi_s.orderlunch.activity.FeedbackActivity;
import com.mobile.liuyi_s.orderlunch.activity.SelectOrderNumActivity;


public class OrderFragment extends Fragment implements RadioGroup.OnCheckedChangeListener, View.OnClickListener{

    private static final String TAG = "OrderFragment";
    private MyApplication mApplication;
    private boolean isOrdered;

    private Toolbar mToolbar;
    private String mSite = "50";
    private TextView mDefaultTextView;
    private TextView mSelectSiteTextView;
    private Button mConfirmButton;

    private RadioGroup mRadioGroup;
    private RadioButton mRadioButtonSite50;
    private RadioButton mRadioButtonSite51;
    private RadioButton mRadioButtonSite52;
    private RadioButton mRadioButtonSite53;
    private RadioButton mRadioButtonSite54;

    public OrderFragment () {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, container, false);
        //用于判断登录后的用户是否已经订餐
        mApplication = ((MyApplication) getActivity().getApplication());
        isOrdered = mApplication.getIsOrdered();
//        Log.d(TAG, "onCreateView: isOrdered:" + isOrdered);
        Utils utils = new Utils();
        initView(view);
//        Log.d(TAG, "onCreateView: isInOrderTime" + utils.isInOrderTime());
//        if (!utils.isInOrderTime() || isOrdered) {
            if (!utils.isInOrderTime()) {
            mRadioGroup.setVisibility(view.INVISIBLE);
            mConfirmButton.setVisibility(view.INVISIBLE);
            mSelectSiteTextView.setVisibility(view.INVISIBLE);
            if (isOrdered) {
                mDefaultTextView.setText("您已订餐");
            }
            if (!utils.isInOrderTime()) {
                mDefaultTextView.setText("订餐时间已结束");
            }
        } else {
            mRadioGroup.setVisibility(view.VISIBLE);
            mConfirmButton.setVisibility(view.VISIBLE);
            mSelectSiteTextView.setVisibility(view.VISIBLE);
            mDefaultTextView.setVisibility(view.INVISIBLE);
            mRadioButtonSite50.setChecked(true);
            mRadioGroup.setOnCheckedChangeListener(this);
            mConfirmButton.setOnClickListener(this);
        }

        return view;
    }

    private void initView (View itemView) {
        mToolbar = ((Toolbar) itemView.findViewById(R.id.toolbar_order_fragment));
        mToolbar.setTitle("");
        setHasOptionsMenu(true);
        ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
        mRadioGroup = ((RadioGroup) itemView.findViewById(R.id.radio_group_order_fragment));
        mRadioButtonSite50 = ((RadioButton) itemView.findViewById(R.id.radio_btn_site50_order_fragment));
        mRadioButtonSite51 = ((RadioButton) itemView.findViewById(R.id.radio_btn_site51_order_fragment));
        mRadioButtonSite52 = ((RadioButton) itemView.findViewById(R.id.radio_btn_site52_order_fragment));
        mRadioButtonSite53 = ((RadioButton) itemView.findViewById(R.id.radio_btn_site53_order_fragment));
        mRadioButtonSite54 = ((RadioButton) itemView.findViewById(R.id.radio_btn_site54_order_fragment));
        mConfirmButton = ((Button) itemView.findViewById(R.id.btn_confirm_order_fragment));
        mSelectSiteTextView = ((TextView) itemView.findViewById(R.id.textview_select_site_order_fragment));
        mDefaultTextView = (TextView) itemView.findViewById(R.id.textview_default_order_fragment);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.order_fragment_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.feedback_menu:
                Intent intent = new Intent(getContext(), FeedbackActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.radio_btn_site50_order_fragment:
                mSite = "50";
                break;
            case R.id.radio_btn_site51_order_fragment:
                mSite = "51";
                break;
            case R.id.radio_btn_site52_order_fragment:
                mSite = "52";
                break;
            case R.id.radio_btn_site53_order_fragment:
                mSite = "53";
                break;
            case R.id.radio_btn_site54_order_fragment:
                mSite = "54";
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm_order_fragment:
                Intent intent = new Intent(getContext(), SelectOrderNumActivity.class);
                intent.putExtra("site", mSite);
                Log.d(TAG, "onClick: 选择站点为：" + mSite);
                startActivity(intent);
                break;
        }
    }
}
